﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Skyhook.Core;

namespace Skyhook.Isometry
{
    internal class WorldPosition : IPositionListener, IPositionChanger
    {
        Vector2 pPosition;
        event WorldPositionChangeEvent OnMove;
        event WorldPositionChangeEvent OnPut;
        bool ExecuteEvents = true;

        public WorldPosition(WorldPositionChangeEvent onput, WorldPositionChangeEvent onmove)
        {
            pPosition = new Vector2(0, 0);
            OnMove += onmove;
            OnPut += onput;
        }

        private void ExecuteOnMoveEvent(Vector2 oldposition)
        {
            if (OnMove != null && ExecuteEvents)
            {
                OnMove(oldposition);
            }
        }

        private void ExecuteOnPutEvent(Vector2 oldposition)
        {
            if (OnMove != null && ExecuteEvents)
            {
                OnPut(oldposition);
            }
        }

        public void DisableEvents()
        {
            ExecuteEvents = false;
        }

        public void EnableEvents()
        {
            ExecuteEvents = true;
        }

        public void Put(float x, float y)
        {
            Vector2 oldposition = pPosition;

            pPosition.X = x;
            pPosition.Y = y;

            ExecuteOnPutEvent(oldposition);
        }

        public void Move(float x, float y)
        {
            Vector2 oldposition = pPosition;

            pPosition.X += x;
            pPosition.Y += y;

            ExecuteOnMoveEvent(oldposition);
        }

        public float X
        {
            get 
            {
                return pPosition.X;
            }
        }

        public float Y
        {
            get
            {
                return pPosition.Y;
            }
        }

        public Vector2 Position
        {
            get
            {
                return pPosition;
            }
        }
    }
}
